package com.lou1052.core.util;

import java.util.Arrays;
import java.util.Collection;

/**
 * Created by liuqing on 14-10-7.
 */
public final class StringUtils {
    private StringUtils(){}
    /**
     * 合并字符串  和String.split()相反
     * @param stringGroup
     * @param mergeString
     * {1,2,3,4}merge“_” = “1_2_3_4”
     * @return
     */
    public static  String merge(Collection<String> stringGroup,String mergeString){
        StringBuilder stringBuilder = new StringBuilder();
        stringGroup.forEach((groupString)->stringBuilder.append(groupString).append(mergeString));
        return removeEnd(stringBuilder.toString(),mergeString);
    }

    /**
     * 合并字符串  和String.split()相反
     * @param stringGroup
     * @param mergeString
     * {1,2,3,4}merge“_” = “1_2_3_4”
     * @return
     */
    public static  String merge(String[] stringGroup,String mergeString){
        StringBuilder stringBuilder = new StringBuilder();
        Arrays.stream(stringGroup).forEach((groupString)->stringBuilder.append(groupString).append(mergeString));
        return removeEnd(stringBuilder.toString(),mergeString);
    }


    /**
     * 截断结尾字符串
     * @param string
     * @param endString
     * @return
     */
    public static String removeEnd(String string,String endString){
        if(string.endsWith(endString)){
            return string.substring(0,string.length()-endString.length());
        }
        throw new IllegalArgumentException(string+"  not endWith  "+endString);
    }
}
